<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\eslib\EsUtils;
use app\model\admin\EsxdmAuthRuleModel;
use think\Request;

class EsxdmAuthRule
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        // 查询 KV 数据
        $appName = $request->param('app', '');
        $mode = $request->param('_mode', '');
        if ($mode && $appName) {
            return json(EsUtils::stdmessage(1, '', EsxdmAuthRuleModel::getDictData($appName)));
        }

        // 查询列表数据
        $searchText = $request->param('search_text', '');
        $page = intval($request->param('page', 1));
        $pageSize = intval($request->param('page_size', 10));
        $data = EsxdmAuthRuleModel::getListData($page, $pageSize, $searchText);
        return json(EsUtils::stdmessage(1, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->param();
        $validator =  \think\facade\Validate::rule([
            'pid|上级类别' => 'require|number',
            'category_name|类别名称' => 'require|max:150',
            'icon|类别图片' => 'max:150',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdmessage(0, $validator->getError()));
        }
        // 表单数据处理
        // ...
        try {
            $model = EsxdmAuthRuleModel::create($formData);
            return json(EsUtils::stdmessage(1, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdmessage(0, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = EsxdmAuthRuleModel::find($id);
            if (!$model) {
                return json(EsUtils::stdmessage(0, '记录不存在'));
            }
        } else {
            $model = new EsxdmAuthRuleModel();
            $model->sort = 999;
            $model->status = 1;
        }
        $data = [
            'data' => $model,
            'status_data' => $model->getStatusDataAttr(),
            'pid_data' => EsxdmAuthRuleModel::getDictData(''),
        ];
        return json(EsUtils::stdmessage(1, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->param();
        $validator =  \think\facade\Validate::rule([
            'pid' => 'require|number',
            'category_name' => 'require|max:150',
            'icon' => 'max:150',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdmessage(0, $validator->getError()));
        }
        try {
            $affected = EsxdmAuthRuleModel::where('id', $id)->update($formData);
            return json(EsUtils::stdmessage(1, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = EsxdmAuthRuleModel::destroy($id);
            return json(EsUtils::stdmessage(1, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdmessage(0, $ex->getMessage()));
        }
    }
}
